// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    // token获取表单显示
    showTokenFrom: false
  },
  // 事件处理函数
  bindViewTap() {
    
  },
  onLoad() {
   
  },
  //token表单处理
  tokenFrom(e){
    //获取自定义(data-status)属性，控制弹出层显示
    var curStatus = e.currentTarget.dataset.status;
    if(curStatus == 'close') {
      this.setData({
        showTokenFrom: false
      });
      //判断表单属性是不是存在，不存在就不请求
      var formValue = e.detail.value;
      if(formValue && formValue.userId && formValue.password){
        var userId = e.detail.value.userId;
        var password = e.detail.value.password;
        this.getToken(userId, password);
      }
    } else if(curStatus == 'open'){
      this.setData({
        showTokenFrom: true
      });
    }
  },
  //请求token接口
  getToken(userId, password) {
    wx.request({
      url: 'http://49.235.32.249:8081/login',
      method: "GET",
      data: {
        userId: userId,
        password: password
      },
      success: (res) => {
        res.data = JSON.stringify(res.data);
        var result = JSON.parse(res.data);
        //成功，并存储到缓存中
        if(result.errCode == 0) {
          var token = result.data;
          wx.setStorageSync('token', token);
        }
        wx.showModal({
          title: '提示',
          content: result.desc,
          showCancel: false
        });
      },
      fail: (res) => {
        //这里一定要先转字符串，不然无法输出
        var result = JSON.stringify(res);
        wx.showModal({
          title: '提示',
          content: result,
          showCancel: false
        });
      }
    })
  },
  gotoTimeConsumePage(e) {
    wx.navigateTo({
      url: '/pages/time-consume/time-consume'
    })
  }
})
